<template>
  <div class="login">
    <video
      src="../../components/视频/bg_video.d2d602a9.mp4"
      class="video"
      muted
      autoplay
      loop
    ></video>
    <div class="background">
      <img
        src="../../components/imgs/微信图片_20200908214215.png"
        alt
        class="imgs"
      />
    </div>
    <el-form
      :model="loginForm"
      status-icon
      :rules="rules"
      ref="loginForm"
      label-width="100px"
      class="demo-loginForm"
    >
      <h1 class="title">千锋管理系统</h1>
      <el-form-item label="用户名:" prop="username">
        <el-input
          type="text"
          v-model="loginForm.username"
          autocomplete="off"
        ></el-input>
      </el-form-item>

      <el-form-item label="密码:" prop="password">
        <el-input
          type="password"
          v-model="loginForm.password"
          autocomplete="off"
        ></el-input>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="submitForm('loginForm')"
          >提交</el-button
        >
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { login } from "@/api";
export default {
  data() {
    return {
      loginForm: {
        username: "",
        password: ""
      },
      rules: {
        username: [{ validator: this.validateUsername, trigger: "blur" }],
        password: [{ validator: this.validatePassword, trigger: "blur" }]
      }
    };
  },
  methods: {
    // 校验用户名规则的函数
    validateUsername(rule, value, callback) {
      if (!value) {
        callback("请输入用户名");
      } else {
        callback();
      }
    },
    // 校验用户名密码规则的函数
    validatePassword(rule, value, callback) {
      //  校验密码：只能输入6-20个字母、数字、下划线
      // var patrn = /^(\w){6,20}$/;
      // if (patrn.test(value)) {
      //   // console.log(123)
      //   callback();
      // } else {
      //   callback("只能输入6-20个字母、数字、下划线");
      // }
      if (value === "") {
        //测试用
        callback("请输入用户名");
      } else {
        callback();
      }
    },
    // 提交按钮触发的事件函数
    submitForm(formName) {
      // console.log(this.$refs);
      this.$refs[formName].validate(valid => {
        // 本地校验通过，valid的值为true
        // 本地校验为通过，valid的值为false

        if (valid) {
          // 打开加载动画
          const loading = this.$loading({
            lock: true,
            text: "正在登录",
            spinner: "el-icon-loading",
            background: "rgba(0, 0, 0, 0.7)"
          });

          // 表示本地校验通过
          // 发送请求登入
          // console.log(this.loginForm.username);
          // console.log(this.loginForm.password);
          let { username, password } = this.loginForm;
          login({ username, password }).then(res => {
            // console.log(res)
            if (res.data.state) {
              //表示请求成功
              let token = res.data.token; //获取token
              // 将token存在本地localstorage
              localStorage.setItem("qf-token", token);
              // 关闭加载器
              loading.close();
              // 将用户信息存到vuex以及localStorage
              this.$store.commit("SET_USERINFO", res.data.userInfo);
              localStorage.setItem(
                "userInfo",
                JSON.stringify(res.data.userInfo)
              );
              // 跳转到主页
              this.$router.push("/");
              this.$message.success("登陆成功，正在跳转");
            } else {
              loading.close();
              this.$message.error("用户名或密码错误");
            }
          });
        } else {
          // console.log(valid);
          console.log("error submit!!");
          return false;
        }
      });
    }
  }
};
</script>

<style>
.login {
  height: 100%;
}
.background {
  position: absolute;

  left: 15%;
  top: 40%;
  z-index: 1;
}
.imgs {
  width: 300px;
  height: 300px;
  opacity: 0.7;
}
.login .el-form {
  width: 400px;
  height: 480px;
  position: absolute;
  left: 60%;
  top: 15%;
  background-color: rgba(0, 0, 0, 0.3);
  padding-top: 70px;
  box-sizing: border-box;
  border-radius: 10px;
  color: white;
}
.login .title {
  text-align: center;
  padding-bottom: 30px;
}
.login .el-form-item__label {
  color: white;
}
.login .el-input__inner {
  width: 250px;
  /* color: white; */
}
.login .el-button {
  width: 250px;
}
.login .video {
  width: 100%;
  height: 100%;
  object-fit: fill;
}
</style>
